using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace MicroMsg.Plugin.DriftBottle.Scene
{
	public class NetScenePickBottle : NetSceneBaseEx<PickBottleRequest, PickBottleResponse, PickBottleRequest.Builder>
	{
		private const string TAG = "NetScenePickBottle";
		public const int STATUS_PICKBOTTLE_COMPLETE = 0;
		public const int STATUS_PICKING_BOTTLE = 1;
		private int mPickBottleStatus;
		private static bool isCanceled;
		public bool doScene()
		{
			NetScenePickBottle.isCanceled = false;
			if (this.mPickBottleStatus == 1)
			{
				return false;
			}
			this.mPickBottleStatus = 1;
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/pickbottle";
			this.mSessionPack.mConnectMode = 2;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(PickBottleRequest request, PickBottleResponse response)
		{
			Log.smethod_1("NetScenePickBottle", "Get my friend failed system error!");
			if (NetScenePickBottle.isCanceled)
			{
				Log.smethod_5("NetScenePickBottle", "Pick bootle has been canceled");
				return;
			}
			this.mPickBottleStatus = 0;
			EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new PickBottleRet(null, -1, BottleEvent.ON_NETSCENE_PICKBOTTLE_FAIL, RetConst.MM_ERR_SYS), null);
		}
		protected override void onSuccess(PickBottleRequest request, PickBottleResponse response)
		{
			if (NetScenePickBottle.isCanceled)
			{
				Log.smethod_5("NetScenePickBottle", "Pick bootle has been canceled");
				return;
			}
			this.mPickBottleStatus = 0;
			if (response == null)
			{
				Log.smethod_1("NetScenePickBottle", "response == null");
				EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new PickBottleRet(null, -1, BottleEvent.ON_NETSCENE_PICKBOTTLE_FAIL, RetConst.MM_ERR_SYS), null);
				return;
			}
			if (response.BaseResponse.Ret != 0)
			{
				RetConst retConst = (RetConst)response.BaseResponse.Ret;
				if (response.BaseResponse.Ret == 19)
				{
					retConst = RetConst.MM_ERR_NO_BOTTLECOUNT;
					DriftBottleServiceCenter.setBottleCount(response.PickCount, -1);
				}
				else
				{
					DriftBottleServiceCenter.setBottleCount(response.PickCount, response.ThrowCount);
				}
				Log.smethod_1("NetScenePickBottle", "Pick Bottle ret is error retcode = " + retConst.ToString());
				EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new PickBottleRet(null, -1, BottleEvent.ON_NETSCENE_PICKBOTTLE_FAIL, retConst), null);
				return;
			}
			DriftBottleServiceCenter.setBottleCount(response.PickCount, response.ThrowCount);
			string bottleInfo = response.BottleInfo;
			BottleContact bottleContact = StorageMgr.bContact.method_0(bottleInfo);
			if (bottleContact == null)
			{
				bottleContact = new BottleContact();
				string[] array = response.BottleInfo.Split(new char[]
				{
					':'
				});
				if (array != null && array.Length > 1)
				{
					bottleContact.strUserName = array[0];
				}
				else
				{
					bottleContact.strUserName = bottleInfo;
				}
			}
			bottleContact.strNickName = response.NickName;
			Dictionary<string, string> dictionary = this.parasUserXml(response.UserInfo);
			if (dictionary == null)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new PickBottleRet(null, -1, BottleEvent.ON_NETSCENE_PICKBOTTLE_FAIL, RetConst.MM_ERR_SYS), null);
				Log.smethod_1("NetScenePickBottle", "parasUserXml userInfoMap == null");
				return;
			}
			string strSignature = "";
			string text = "";
			string strCity = "";
			string strProvince = "";
			dictionary.TryGetValue("signature", ref strSignature);
			dictionary.TryGetValue("sex", ref text);
			dictionary.TryGetValue("city", ref strCity);
			dictionary.TryGetValue("province", ref strProvince);
			bottleContact.strSignature = strSignature;
			bottleContact.nSex = int.Parse(text);
			bottleContact.strCity = strCity;
			bottleContact.strProvince = strProvince;
			bottleContact.nCreateTime = (long)Util.getNowMilliseconds();
			bottleContact.nImgFlag = 4;
			StorageMgr.bContact.update(bottleContact);
			Log.smethod_4("NetScenePickBottle", "Pick Bottle success!");
			EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new PickBottleRet(response.BottleInfo, response.MsgType, BottleEvent.ON_NETSCENE_PICKBOTTLE_SUCCESS, RetConst.MM_OK), null);
			DriftBottleServiceCenter.sceneOpenBottle.doScene(response.BottleInfo, response.MsgType);
			List<string> list = new List<string>();
			list.Add(bottleContact.strUserName);
			ServiceCenter.sceneBatchGetContact.doScene(list);
		}
		public static void cancelPick()
		{
			Log.smethod_5("NetScenePickBottle", "user has canceled");
			NetScenePickBottle.isCanceled = true;
		}
		public static bool isPickCanceled()
		{
			return NetScenePickBottle.isCanceled;
		}
		public Dictionary<string, string> parasUserXml(string strXml)
		{
			Dictionary<string, string> dictionary = null;
			Dictionary<string, string> result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("userinfo");
				if (xElement != null)
				{
					dictionary = new Dictionary<string, string>();
					string value = xElement.Attribute("signature").get_Value();
					string value2 = xElement.Attribute("sex").get_Value();
					string value3 = xElement.Attribute("city").get_Value();
					string value4 = xElement.Attribute("province").get_Value();
					dictionary.Add("signature", value);
					dictionary.Add("sex", value2);
					dictionary.Add("city", value3);
					dictionary.Add("province", value4);
				}
				result = dictionary;
			}
			catch (Exception ex)
			{
				Log.smethod_1("NetScenePickBottle", ex.ToString());
				result = null;
			}
			return result;
		}
	}
}
